package com.service.report;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Iterator;
import java.util.Vector;

import com.bpms.common.CommonUtil;
import com.bpms.common.Param;
import com.bpms.common.Tools;
import com.bpms.common.VO;
import com.bpms.common.VOList;
import com.bpms.esb.IService;


public class Hdprintreport implements IService{

	@SuppressWarnings("unchecked")
	public VO exec(VO arg0) {
		VO result = new VO();
		VOList relist = new VOList();
		try{
			VOList list = (VOList) arg0.getObj("volist");
			Vector v = list.listData;
			Iterator it = v.iterator();
			while(it.hasNext()){
				VO vo = (VO)it.next();
				VO temp = new VO();
				String date = new SimpleDateFormat("yyyyMMdd").format(new Date());      //打印日期
				String yyyy = date.substring(0, 4);
				String mm = date.substring(4, 6);
				String dd = date.substring(6);
				
				temp.setAttr("yyyy", yyyy); //年
				temp.setAttr("mm", mm);     //月
				temp.setAttr("dd", dd);     //日
				temp.setAttr("CQC", vo.getAttr("PAYEENAME"));  //出票人全称
				temp.setAttr("SQC", vo.getAttr("DRAWeeNAME"));  //收款人全称
				temp.setAttr("CZH", vo.getAttr("PAYEEACCNO")); 	   //出票人账号	
				temp.setAttr("SZH", vo.getAttr("DRAWEEACCNO"));   //收款人账号
				temp.setAttr("CKHYH", vo.getAttr("PAYEEBANK")); //出票人开户行
				temp.setAttr("SKHYH", vo.getAttr("DRAWEEBANK"));  //收款人开户行
				String money = vo.getAttr("AMOUNT"); //金额
				temp.setAttr("JE",Tools.numtochinese(money));  //金额
				String[] attr = toNum(money);	//金额
				
				temp.setAttr("F", attr[1]); 	//分
				temp.setAttr("J", attr[2]); 	//角
				temp.setAttr("Y", attr[4]); 	//元
				temp.setAttr("S", attr[5]); 	//十
				temp.setAttr("B", attr[6]); 	//白
				temp.setAttr("Q", attr[7]); 	//千
				temp.setAttr("W", attr[8]); 	//万
				temp.setAttr("SW", attr[9]); 	//十万
				temp.setAttr("BW", attr[10]); 	//百万
				temp.setAttr("QW", attr[11]); 	//千万
				temp.setAttr("Yi", attr[12]); 	//亿
			
				CommonUtil.debug("get trantype from param ATM.TRANTYPE" + vo.getAttr("TRANTYPE"));
				String pjzl = Param.getValue("ATM", "TRANTYPE" + vo.getAttr("TRANTYPE"));
				temp.setAttr("PJZL", pjzl); //票据种类  01本行支票转账、02他行支票转账
				
				temp.setAttr("PJHM", vo.getAttr("BILLNO")); //票据号码    凭证号
				temp.setAttr("PJZS", "1");  	//票据数量
				temp.setAttr("FHJZ", " "); 		//复核记账
				temp.setAttr("FH", "" );  		//复核
				temp.setAttr("JZ", " ");  		//记账
				temp.setAttr("KHYHQZ", " "); 	//开户银行签章
				
				relist.listData.add(temp);
			}
			result.setObj("list", relist);
		}catch(Exception e){
			CommonUtil.debug("HdPrintReport exception");
		}
		return result;
	}

	String[] toNum(String str){
		String s = "";
		String[] attr = new String[]{};
		for(int i = str.length(); i>=0 ;i--){
			if(i+1>str.length()){
				s = str.substring(i);
			}else{
				s = str.substring(i, i+1);
			}
			//attr = Arrays.copyOf(attr, attr.length + 1);
			attr[attr.length-1] = s;
		}
		//attr = Arrays.copyOf(attr, attr.length + 1);
		attr[attr.length-1] = "￥";
		int len = attr.length;
		if(len<15){
			for(int j = len;j<=15;j++){
				//attr = Arrays.copyOf(attr, attr.length + 1);
				attr[j] = " ";
			}
		}
		return attr;
	}
	
//	public static String code2code(String strIn, String sourceCode,
//			String targetCode) {
//		String strOut = null;
//		if (strIn == null || (strIn.trim()).equals(""))
//			return strIn;
//		try {
//			byte[] b = strIn.getBytes(sourceCode);
//			for (int i = 0; i < b.length; i++) {
//			}
//			strOut = new String(b, targetCode);
//		} catch (Exception e) {
//			CommonUtil.debug("HdPrintReport code2code error");
//			return null;
//		}
//		return strOut;
//	}
	
}
